package com.dalanmao.netty2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {


    private TextView request;

    private TextView send, close, send0, download;


    private TimeClient client;


    public static final String host = "192.168.100.103";
    int port = 60000;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        request = (TextView) findViewById(R.id.request);
        send = (TextView) findViewById(R.id.send);
        send0 = (TextView) findViewById(R.id.send0);
        close = (TextView) findViewById(R.id.close);
        download = (TextView) findViewById(R.id.close);


        request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        client = new TimeClient();
                        client.connect(host, port);

                    }
                }).start();


            }
        });


        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String login = "{\"login\":[\"admin\",\"admin\"]}";
                client.sendMessage(login);

                String monit = "{\"enmonitor\":\"on\"}";   /////打开监控
                client.sendMessage(monit);

                String open = "{\"devdpmsg\":{\"devid\":17498235,\"dpid\":1,\"data\":1}}";
                client.sendMessage(open);

            }
        });

        send0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String open = "{\"devdpmsg\":{\"devid\":17498235,\"dpid\":1,\"data\":0}}";
                client.sendMessage(open);
            }
        });


        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                client.close();

            }
        });


        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String download = "{\"filedownreq\":\"mibee.json\"}";
                client.sendMessage(download);


            }
        });


    }


    private void connect() {


        try {
            Socket socket = new Socket("192.168.100.103", 60000);


            Log.i("zl", "connect is success...  ");

        } catch (IOException e) {

            e.printStackTrace();
            Log.i("zl", "connect failed...  ");
        } catch (Exception e) {

            Log.i("zl", "connect Exception...  ");
            e.printStackTrace();
        }


    }


}
